﻿using Janus.BaseEntitys;
using Janus.Cores;
using Janus.ExpressionSerialization;
using Janus.Mes.DTO;
using Janus.Mes.POCO;
using Janus.Mes.Repositories.iBatis;
using Janus.Mes.SqlDal;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Tests.Janus.Mes.SqlDal.ConsWrite
{
    public class IndicatorDalTest
    {
        public IndicatorDalTest()
        {
            //this.Insert();
            //this.InsertMany();
            //this.Update();
            //this.UpdateMany();
            //this.UpdateSetion();
            //this.UpdateSetion2();
           // this.UpdateSetion3();
           // this.Save1();
            //this.Save2();
            //this.SaveMany();
            //this.Delete();
            //this.Delete2();
            //this.Delete3();
            //this.FindOne();
            //this.FindScalar();
            //this.IsExists();
            //this.FindALL();
            this.FindALLForPage();
            //this.FindALLForPagePro();
        }

        private IndicatorDal dal = new IndicatorDal();

        private void Insert()
        {
            var model = new Indicator();
            model.IndicatorName = "测试Insert" + DateTime.Now.ToString("yyyyMMddHH");
            model.IndicatorFiledName = "IndicatorFiledName" + DateTime.Now.ToString("yyyyMMddHH");
            //model.IndicatorValue = 123;
            model.CreateDt = DateTime.Now;
            var result = dal.Add(model);
            Trace.WriteLine("Insert()测试:" + result);
        }

        private void InsertMany()
        {
            var models = new List<Indicator>();
            for (int i = 1; i <= 10; i++)
            {
                var model = new Indicator();
                model.IndicatorName = "测试InsertMany" + i.ToString();
                model.IndicatorFiledName = "IndicatorFiledName" + i.ToString();
                model.IndicatorValue = i;
                model.CreateDt = DateTime.Now;
                models.Add(model);
            }
            var result = dal.Add(models);
            Trace.WriteLine("Insert()测试:" + result);
        }

        private void Update()
        {
            var model = new Indicator();
            //model.Id = 1;
            model.IndicatorName = "测试Update" + DateTime.Now.ToString("yyyyMMddHH");
            model.IndicatorFiledName = "IndicatorFiledName" + DateTime.Now.ToString("yyyyMMddHH");
            //model.IndicatorValue = 123;
            model.CreateDt = DateTime.Now;
            var result = dal.Update(model);
            Trace.WriteLine("Update()测试:" + result);
        }
        private void UpdateMany()
        {
            var models = new List<Indicator>();
            for (int i = 1; i <= 10; i++)
            {
                var model = new Indicator();
               // model.Id = i;
                model.IndicatorName = "测试UpdateMany" + i.ToString();
                model.IndicatorFiledName = "IndicatorFiledName" + i.ToString();
                model.IndicatorValue = i;
                model.CreateDt = DateTime.Now;
                models.Add(model);
            }
            var result = dal.Update(models);
            Trace.WriteLine("UpdateMany()测试:" + result);
        }

        private void UpdateSetion()
        {
            dynamic model = new DynamicEntity();
            model.IndicatorName = "UpdateSetion test";
            var condition = new Conditioner();
            condition.Add(null, "(", ")", "Id=1", null);
            bool result = dal.UpdateSetion(model, condition) > 0;
            Trace.WriteLine("UpdateSetion()测试:" + result);
        }

        private void UpdateSetion2()
        {
            Indicator model = new Indicator();
            model.IndicatorName = "UpdateSetion test";
            var condition = new Conditioner();
            condition.Add(null, "(", ")", "Id=2", null);
            bool result = dal.UpdateSetion(model, condition) > 0;
            Trace.WriteLine("UpdateSetion2()测试:" + result);
        }

        private void UpdateSetion3()
        {
            string sss = "test";
            //bool result = dal.UpdateSetion<Indicator>(c => new Indicator() { IndicatorName = "UpdateSetion<Indicator>" }, c => c.Id == 2) > 0;
            while (true)
            {
                Stopwatch wathc = new Stopwatch();
                wathc.Start();
                Expression<Func<Indicator, Indicator>> updater = c => new Indicator() { IndicatorName = sss };
                var updaterxml = SerializeHelper.SerializeExpression(updater);
                wathc.Stop();
                Console.WriteLine(wathc.ElapsedMilliseconds);
                wathc.Restart();
                updater = SerializeHelper.DeserializeExpression<Indicator, Indicator>(updaterxml);
                wathc.Stop();
                Console.WriteLine(wathc.ElapsedMilliseconds);

                //wathc.Restart();
                //Expression<Func<Indicator, bool>> p = (c) => true;
                //var pXml = SerializeHelper.SerializeExpression(p);
                //p = SerializeHelper.DeserializeExpression<Indicator, bool>(pXml);
               // bool result = dal.UpdateSetion<Indicator>(updater, p) > 0;
               // wathc.Stop();
               // Console.WriteLine(wathc.ElapsedMilliseconds);
               // Trace.WriteLine("UpdateSetion3()测试:" + result);
            }
        }

        private void Save1()
        {
            var model = new Indicator();
            model.IndicatorName = "测试Save" + DateTime.Now.ToString("yyyyMMddHH");
            model.IndicatorFiledName = "Save" + DateTime.Now.ToString("yyyyMMddHH");
            model.IndicatorValue = 123;
            model.CreateDt = DateTime.Now;
            //model.Id = 1;
            var result = dal.Save(model);
            Trace.WriteLine("Save1()测试:" + result);
        }
        private void Save2()
        {
            var model = new Indicator();
            model.IndicatorName = "测试Save" + DateTime.Now.ToString("yyyyMMddHH");
            model.IndicatorFiledName = "Save" + DateTime.Now.ToString("yyyyMMddHH");
            model.IndicatorValue = 123;
            model.CreateDt = DateTime.Now;
            var result = dal.Save(model);
            Trace.WriteLine("Save2()测试:" + result);
        }

        private void SaveMany()
        {
            var models = new List<Indicator>();
            for (int i = 1; i <= 10; i++)
            {
                var model = new Indicator();
                //model.Id = i;
                model.IndicatorName = "SaveMany" + i.ToString();
                model.IndicatorFiledName = "SaveMany" + i.ToString();
                model.IndicatorValue = i;
                model.CreateDt = DateTime.Now;
                models.Add(model);
            }
            for (int i = 1; i <= 10; i++)
            {
                var model = new Indicator();
                model.IndicatorName = "SaveMany add" + i.ToString();
                model.IndicatorFiledName = "SaveMany add" + i.ToString();
                model.CreateDt = DateTime.Now;
                models.Add(model);
            }
            var result = dal.Save(models);
            Trace.WriteLine("SaveMany()测试:" + result);
        }

        private void Delete()
        {
            var result = dal.Delete(8632);
            Trace.WriteLine("Delete()测试:" + result);
        }

        private void Delete2()
        {
            Indicator model = new Indicator();
            model.IndicatorName = "Delete2 test";
            var condition = new Conditioner();
            condition.Add(null, "(", ")", "[IndicatorName]='SaveMany add8'", "AND");
            condition.Add(null, "(", ")", "[IndicatorFiledName]='SaveMany add8'", null);
            bool result = dal.Delete(condition);
            Trace.WriteLine("Delete()测试:" + result);
        }
        private void Delete3()
        {
            bool result = dal.Delete<Indicator>(c => c.IndicatorName.StartsWith("SaveMany add7"));
            Trace.WriteLine("Delete<Indicator>()测试:" + result);
        }

        private void FindOne()
        {
            var result = dal.FindOne<Indicator>(1);
            Trace.WriteLine("FindOne()测试:" + result != null);

        }

        private void FindScalar()
        {
            //var result = dal.FindScalar<Indicator>(p => p.IndicatorName, c => c.Id == 5);
            //Trace.WriteLine("FindScalar()测试:" + result);
        }

        private void IsExists()
        {
            //var result = dal.IsExists<Indicator>(c => c.Id == 5);
           // Trace.WriteLine("IsExists()测试:" + result);
        }

        private void FindALL()
        {
            var result = dal.FindAll<Indicator>(c => c.IndicatorName.StartsWith("SaveMany") && c.IndicatorValue > 1);
            Trace.WriteLine("FindALL()测试:" + (result.Count > 0));
        }

        private void FindALLForPage()
        {
            PaginationDTO pagination = new PaginationDTO();
            SearchEntity<Indicator> searchEntity = new SearchEntity<Indicator>(null, pagination);
            Querier querier = new Querier();
            querier.Where("IndicatorName.StartsWith(\"SaveMany\") || IndicatorValue > 1");
            querier.Where("IndicatorFiledName.Contains(\"SaveMany1\")");
            querier.OrderBy<Indicator>(c => c.CreateDt).OrderDescBy<Indicator>(c => c.Id);
            searchEntity.Queriers = querier;
            //(c => (c.IndicatorName.StartsWith("SaveMany") || c.IndicatorValue > 1) && c.IndicatorFiledName.Contains("SaveMany1"), pagination);
            //SearchEntity<Indicator> searchEntity = new SearchEntity<Indicator>(null, pagination);
            //searchEntity.OrderBy(c => c.CreateDt).OrderDescBy(c => c.Id);
            var result = dal.FindAll(searchEntity);
            Trace.WriteLine("FindALLForPage()测试:" + (result != null));
        }

        private void FindALLForPagePro()
        {
            PaginationDTO pagination = new PaginationDTO();
            PageCriteriaer<Indicator> searchEntity = new PageCriteriaer<Indicator>(c => (c.IndicatorName.StartsWith("SaveMany") || c.IndicatorValue > 1) && c.IndicatorFiledName.Contains("SaveMany1"), pagination, "Id");
            searchEntity.OrderBy(c => c.CreateDt).OrderDescBy(c => c.Id);
            var result = dal.FindAllByPro<Indicator>(searchEntity.PageCriteria);
            Trace.WriteLine("FindALLForPage()测试:" + (result != null));
        }
    }

}
